/*
 * Copyright (c) 2019 Mike Penz
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.mikepenz.iconics.view;

import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.components.Text;
import ohos.agp.components.element.PixelMapElement;
import ohos.app.Context;
import ohos.media.image.common.Size;

import com.mikepenz.iconics.IconicsDrawable;
import com.mikepenz.iconics.IconicsDrawableConverter;
import com.mikepenz.iconics.internal.CompoundIconicsDrawables;
import com.mikepenz.iconics.internal.IconicsViewsAttrsApplier;

/**
 * 带选中状态的text
 *
 * @since 2021-06-16
 */
public class IconicsCheckableTextView extends Text implements CompoundIconicsDrawables {
    private final PixelMapElement[] elements = new PixelMapElement[4];
    private final PixelMapElement[] checkedElements = new PixelMapElement[4];
    private boolean isChecked = false;

    /**
     * 构造函数
     *
     * @param context
     * @param attrSet
     */
    public IconicsCheckableTextView(Context context, AttrSet attrSet) {
        super(context, attrSet);
        init(context, attrSet);
    }

    /**
     * 构造函数
     *
     * @param context
     * @param attrSet
     * @param styleName
     */
    public IconicsCheckableTextView(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
        init(context, attrSet);
    }

    private void init(Context context, AttrSet attrSet) {
        setClickedListener(new ClickedListener() {
            @Override
            public void onClick(Component component) {
                isChecked = !isChecked;
                refresh();
            }
        });
        IconicsDrawable drawable = IconicsViewsAttrsApplier.getIconicsTextDrawable(context, attrSet);
        if (drawable != null) {
            setDrawableForAll(drawable);
            return;
        }
        IconicsDrawable start = IconicsViewsAttrsApplier.getIconicsTextStartDrawable(context, attrSet);
        if (start != null) {
            setIconicsDrawableStart(start);
        }
        IconicsDrawable top = IconicsViewsAttrsApplier.getIconicsTextTopDrawable(context, attrSet);
        if (top != null) {
            setIconicsDrawableTop(top);
        }
        IconicsDrawable end = IconicsViewsAttrsApplier.getIconicsTextEndDrawable(context, attrSet);
        if (end != null) {
            setIconicsDrawableEnd(end);
        }
        IconicsDrawable bottom = IconicsViewsAttrsApplier.getIconicsTextBottomDrawable(context, attrSet);
        if (bottom != null) {
            setIconicsDrawableBottom(bottom);
        }

        IconicsDrawable drawableChecked = IconicsViewsAttrsApplier.getIconicsTextCheckedDrawable(context, attrSet);
        if (drawableChecked != null) {
            setDrawableForAllChecked(drawableChecked);
            return;
        }
        IconicsDrawable startChecked = IconicsViewsAttrsApplier.getIconicsTextStartCheckedDrawable(context, attrSet);
        if (startChecked != null) {
            setIconicsDrawableStartChecked(startChecked);
        }
        IconicsDrawable topChecked = IconicsViewsAttrsApplier.getIconicsTextTopCheckedDrawable(context, attrSet);
        if (topChecked != null) {
            setIconicsDrawableTopChecked(topChecked);
        }
        IconicsDrawable endChecked = IconicsViewsAttrsApplier.getIconicsTextEndCheckedDrawable(context, attrSet);
        if (endChecked != null) {
            setIconicsDrawableEndChecked(endChecked);
        }
        IconicsDrawable bottomChecked = IconicsViewsAttrsApplier.getIconicsTextBottomCheckedDrawable(context, attrSet);
        if (bottomChecked != null) {
            setIconicsDrawableBottomChecked(bottomChecked);
        }
    }

    /**
     * 刷新checkbox状态
     */
    protected void refresh() {
        if (isChecked) {
            setAroundElements(checkedElements[0], checkedElements[1], checkedElements[2], checkedElements[3]);
        } else {
            setAroundElements(elements[0], elements[1], elements[2], elements[3]);
        }
    }

    @Override
    public void setIconicsDrawableStart(IconicsDrawable icon) {
        PixelMapElement element = IconicsDrawableConverter.converter(icon,
            new Size(icon.getSizeXpx(), icon.getSizeYpx()));
        elements[0] = element;
        refresh();
    }

    @Override
    public void setIconicsDrawableTop(IconicsDrawable icon) {
        PixelMapElement element = IconicsDrawableConverter.converter(icon,
            new Size(icon.getSizeXpx(), icon.getSizeYpx()));
        elements[1] = element;
        refresh();
    }

    @Override
    public void setIconicsDrawableEnd(IconicsDrawable icon) {
        PixelMapElement element = IconicsDrawableConverter.converter(icon,
            new Size(icon.getSizeXpx(), icon.getSizeYpx()));
        elements[2] = element;
        refresh();
    }

    @Override
    public void setIconicsDrawableBottom(IconicsDrawable icon) {
        PixelMapElement element = IconicsDrawableConverter.converter(icon,
            new Size(icon.getSizeXpx(), icon.getSizeYpx()));
        elements[3] = element;
        refresh();
    }

    @Override
    public void setDrawableForAll(IconicsDrawable icon) {
        PixelMapElement element = IconicsDrawableConverter.converter(icon,
            new Size(icon.getSizeXpx(), icon.getSizeYpx()));
        elements[0] = element;
        elements[1] = element;
        elements[2] = element;
        elements[3] = element;
        refresh();
    }

    private void setIconicsDrawableStartChecked(IconicsDrawable icon) {
        PixelMapElement element = IconicsDrawableConverter.converter(icon,
            new Size(icon.getSizeXpx(), icon.getSizeYpx()));
        checkedElements[0] = element;
        refresh();
    }

    private void setIconicsDrawableTopChecked(IconicsDrawable icon) {
        PixelMapElement element = IconicsDrawableConverter.converter(icon,
            new Size(icon.getSizeXpx(), icon.getSizeYpx()));
        checkedElements[1] = element;
        refresh();
    }

    private void setIconicsDrawableEndChecked(IconicsDrawable icon) {
        PixelMapElement element = IconicsDrawableConverter.converter(icon,
            new Size(icon.getSizeXpx(), icon.getSizeYpx()));
        checkedElements[2] = element;
        refresh();
    }

    private void setIconicsDrawableBottomChecked(IconicsDrawable icon) {
        PixelMapElement element = IconicsDrawableConverter.converter(icon,
            new Size(icon.getSizeXpx(), icon.getSizeYpx()));
        checkedElements[3] = element;
        refresh();
    }

    private void setDrawableForAllChecked(IconicsDrawable icon) {
        PixelMapElement element = IconicsDrawableConverter.converter(icon,
            new Size(icon.getSizeXpx(), icon.getSizeYpx()));
        checkedElements[0] = element;
        checkedElements[1] = element;
        checkedElements[2] = element;
        checkedElements[3] = element;
        refresh();
    }
}
